package com.dhcc.core.modules.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.modules.system.cache.permission.IPermissionCache;
import com.dhcc.core.modules.system.service.IPermissionService;

/**
 * 权限控制器
 * 
 * @ClassName: PermissionController
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:30:31
 */
@Controller
@RequestMapping("${sys.backendPath}/permission")
public class PermissionController extends BaseController {

    private static String PREFIX = "/backend/system/permission/";

    @Autowired
    IPermissionService permissionService;
    
    @Value("${sys.orgFullName}")
    private String orgFullName;

    /**
     * 跳转到资源受权页面
     */
    @RequestMapping("/resourceAuth")
    public String resourceAuth() {
        return PREFIX + "resourceAuth.html";
    }

    /**
     * 跳转到资源受权页面-反向
     */
    @RequestMapping("/principalAuth")
    public String principalAuth() {
        return PREFIX + "principalAuth.html";
    }

    @RequestMapping(value = "/loadPermsByPrincipalId")
    @ResponseBody
    public R loadPermsByPrincipalId(@RequestParam Long principalId, @RequestParam Integer principalType) {
        Map<String,List<Long>> res = this.permissionService.loadPermsByPrincipalId(principalId,principalType);
        return R.ok().put("data", res);
    }

    @RequestMapping(value = "/loadPermsByResourceId")
    @ResponseBody
    public R loadPermsByResourceId(@RequestParam Long resourceId) {
        Map<String,List<Long>> res = this.permissionService.loadPermsByResourceId(resourceId);
        return R.ok().put("data", res);
    }

    @RequestMapping(value = "/auth")
    @ResponseBody
    @CacheClear(IPermissionCache.CACHE_NAME)
    public R auth(@RequestParam Long resourceId,@RequestParam Long principalId, @RequestParam Integer principalType,@RequestParam Boolean isAdd) {
        this.permissionService.authPerm(resourceId,principalId,principalType,isAdd);
        return R.ok();
    }
}
